using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

using Pegasus.Diagnostics;

namespace Pegasus
{
	/// <summary>
	/// 
	/// </summary>
	[Serializable]
	public class PegasusExceptionList : PegasusException
	{
		// Local Instance Values
		private Exception m_primaryException = null;
		private List<Exception> m_exceptionList = new List<Exception>();

		/// <summary>
		/// Initializes a new instance of the <see cref="T:WorkflowNotFoundException"/> class.
		/// </summary>
		public PegasusExceptionList()
		{
		}
		
		/// <summary>
		/// Initializes a new instance of the <see cref="T:WorkflowNotFoundException"/> class.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="args">The args.</param>
		public PegasusExceptionList( string message, params object[] args ) :
			base( string.Format( message, args ) )
		{
			// Check Parameters
			ParamCode.AssertNotEmpty( message, "message" );
			ParamCode.AssertNotNull( args, "args" );
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="T:WorkflowNotFoundException"/> class.
		/// </summary>
		/// <param name="info">The info.</param>
		/// <param name="context">The context.</param>
		protected PegasusExceptionList( SerializationInfo info, StreamingContext context ) :
			base( info, context )
		{
			// Check Parameters
			ParamCode.AssertNotNull( info, "info" );
			ParamCode.AssertNotNull( context, "context" );

			m_exceptionList = (List<Exception>) info.GetValue( "ExceptionList", typeof( List<Exception> ) );
		}

		/// <summary>
		/// Gets the count of exceptions in the list.
		/// </summary>
		/// <value>The count.</value>
		public int Count
		{
			get
			{
				return m_exceptionList.Count;
			}
		}

		/// <summary>
		/// Gets or sets the primary exception.
		/// </summary>
		/// <value>The primary exception.</value>
		public Exception PrimaryException
		{
			get
			{
				return m_primaryException;
			}

			set
			{
				m_primaryException = value;
			}
		}

		/// <summary>
		/// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> with information about the exception.
		/// </summary>
		/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual information about the source or destination.</param>
		/// <exception cref="T:System.ArgumentNullException">The info parameter is a null reference (Nothing in Visual Basic). </exception>
		/// <PermissionSet><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/></PermissionSet>
		public override void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			// Check Parameters
			ParamCode.AssertNotNull( info, "info" );
			ParamCode.AssertNotNull( context, "context" );

			base.GetObjectData( info, context );
			info.AddValue( "ExceptionList", m_exceptionList );
		}

		/// <summary>
		/// Adds the exception.
		/// </summary>
		/// <param name="e">The e.</param>
		public void AddException( Exception e )
		{
			// Check Parameters
			ParamCode.AssertNotNull( e, "e" );

			m_exceptionList.Add( e );
		}
	}
}
